package cxf.spring.demo.service;

import cxf.spring.demo.domain.message.Message;
import cxf.spring.demo.domain.message.MessagesList;
import cxf.spring.demo.storage.ServerStorage;

import javax.jws.WebService;

/**
 * User: Vlad Vinichenko (akerigan@gmail.com)
 * Date: 06.03.2009
 * Time: 22:00:12
 */
@WebService(endpointInterface = "cxf.spring.demo.service.MessageService")
public class MessageServiceImpl implements MessageService {

    ServerStorage serverStorage;

    public void setServerStorage(ServerStorage serverStorage) {
        this.serverStorage = serverStorage;
    }

    void checkUser(String login) throws Exception{
        if (serverStorage.getUser(login) == null) {
            throw new IllegalArgumentException("User not found");
        }
    }

    public MessagesList getNewMessages(String login) throws Exception {
        checkUser(login);
        return serverStorage.getMessages(login);
    }

    public Message getMessage(String login, int messageId) throws Exception {
        checkUser(login);
        return serverStorage.getMessage(login, messageId);
    }

    public void sendMessage(String login, Message message) throws Exception {
        checkUser(login);
        serverStorage.sendMessage(login, message);
    }

    public void delete(String login, MessagesList messagesList) throws Exception {
        checkUser(login);
        serverStorage.delete(login, messagesList);
    }
}
